package com.mathdroid.gui.main.character;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;

import javax.swing.JPanel;

import com.mathdroid.gui.ImageLoader;
import com.mathdroid.model.EModelObserverEvent;
import com.mathdroid.model.IModelObserver;
import com.mathdroid.model.Model;
import com.mathdroid.model.enums.EItemSlot;
import com.mathdroid.model.enums.EItemType;
import com.mathdroid.model.items.Item;

/**
 * An item slot is a JPanel and shows the icon of the item and its 
 * modifications, crystals, etc. This panel has a variable size, but contains
 * another panel with fixed size, that is centered both horizontal and 
 * vertical. 
 */
public final class ItemSlot extends JPanel implements IModelObserver {

	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The centered content of this panel.
	 */
	private final JPanel content = new JPanel();
	
	/**
	 * The width and height of the buttons.
	 */
	private static final int BUTTON_EDGE = 50;
	
	/**
	 * The width of the module button.
	 */
	private static final int MODULE_WIDTH = 22;
	
	/**
	 * The item button.
	 */
	private ItemButton itemButton = null;
	
	/**
	 * The image loader.
	 */
	private final ImageLoader il = new ImageLoader();
	
	/**
	 * The slot type.
	 */
	private EItemSlot slot = null;
	
	/**
	 * The type of the item in this slot.
	 */
	private EItemType type = null;
	
	/**
	 * Creates an item slot.
	 * @param s The item slot type.
	 * @param t The type of the item in this slot.
	 */
	public ItemSlot(final EItemSlot s, final EItemType t) {
		super();
		
		this.slot = s;
		this.type = t;
		
		Model.registerObserver(this);
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.CENTER;
		c.gridx = 0;
		c.gridy = 0;
		
		this.content.setPreferredSize(new Dimension(BUTTON_EDGE + MODULE_WIDTH,
				BUTTON_EDGE));
		this.content.setMinimumSize(new Dimension(BUTTON_EDGE + MODULE_WIDTH,
				BUTTON_EDGE));

		this.add(this.content, c);
		this.buildContent();
	}

	/**
	 * Align the buttons.
	 */
	private void buildContent() {
		this.content.setLayout(null);

		this.buildItemButton();
		this.buildModulesButton();
	}

	/**
	 * Build the button to edit the modules / enhance the item.
	 */
	private void buildModulesButton() {
		final ItemEnhancementButton button = 
			new ItemEnhancementButton(this.slot);
		button.setBounds(new Rectangle(BUTTON_EDGE, 0,
				MODULE_WIDTH, BUTTON_EDGE));
		
		this.content.add(button);
	}

	/**
	 * Build and add the item button.
	 */
	private void buildItemButton() {
		this.itemButton = new ItemButton(this.slot, null, this.type);
		this.itemButton.setBounds(new Rectangle(0, 0,
				BUTTON_EDGE, BUTTON_EDGE));
		
		this.content.add(this.itemButton);
	}

	@Override
	public void modelChanged(final EModelObserverEvent e) {
		if (e == EModelObserverEvent.PROFILE_LOADED
				|| e == EModelObserverEvent.EQUIPPED_ITEMS_CHANGED
				|| e == EModelObserverEvent.EQUIPPED_ITEMS_LOADED) {
			this.refresh();
		}
	}

	/**
	 * Refresh the icons of this item slot.
	 */
	private void refresh() {
		Item item = Model.getProfile().getEquipedItems().get(this.slot);

		if (item != null) {
			this.itemButton.setIcon(
					this.il.getItemIcon(item.getImageName()));
		} else {
			this.itemButton.setIcon(this.il.getImage(this.slot.getIcon()));
		}
	}
}
